﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Slex {

    public class Area : Grid {        
        public Orientation Orientation { get; set; }
        public void AddRow(params FrameworkElement[] ps) {
            Orientation = System.Windows.Controls.Orientation.Vertical;
            this.Children.Clear();
            var len = ps.Length;
            this.RowDefinitions.Clear();
            for (var i = 0; i < len; i++) {
                if (ps[i].Height!=double.NaN&&ps[i].Height > 0.1d) {
                    this.RowDefinitions.Add(new RowDefinition { Height = new GridLength(ps[i].Height,GridUnitType.Pixel) });
                } else {
                    this.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1.0d, GridUnitType.Star) });
                }
                this.Children.Add(ps[i]);
                Grid.SetRow(ps[i], i);
            }
        }

        public void AddColum(params FrameworkElement[] ps) {
            Orientation = System.Windows.Controls.Orientation.Horizontal;
            this.Children.Clear();
            var len = ps.Length;
            this.ColumnDefinitions.Clear();
            for (var i = 0; i < len; i++) {
                if (ps[i].Width != double.NaN&&ps[i].Width > 0.1d) {
                    this.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(ps[i].Width, GridUnitType.Pixel) });
                } else {
                    this.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0d, GridUnitType.Star) });
                }
                this.Children.Add(ps[i]);
                Grid.SetColumn(ps[i], i);
            }
        }
        public void RemoveAll() {
            this.Children.Clear();
        }
        public virtual void AddOne(FrameworkElement fe) {
            this.Children.Clear();
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();            
            this.Children.Add(fe);
        }
        public void AddTo(FrameworkElement fe) {
            fe.Margin = new Thickness(0);
            fe.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            fe.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            Area.SetColumn(fe, 0);
            Area.SetRow(fe, 0);
            Area.SetColumnSpan(fe, this.ColumnDefinitions.Count == 0 ? 1 : this.ColumnDefinitions.Count);
            Area.SetRowSpan(fe, this.RowDefinitions.Count == 0 ? 1 : this.RowDefinitions.Count);            
            this.Children.Add(fe);
        }
        public void ClearAll() {
            var children = new List<FrameworkElement>(); 
            foreach (FrameworkElement item in Children) {
                children.Add(item);
            }
            foreach (var item in children) {
                var p = item.Parent as Area;
                p.Children.Remove(item);
            }
            this.Children.Clear();
            this.ColumnDefinitions.Clear();
            this.RowDefinitions.Clear();
        }
    }
}
